/**
 * 
 */
package com.cnhaimei.vod.net;

import com.cnhaimei.com.VODLog;
import com.cnhaimei.vod.logic.EventsManager;
import com.cnhaimei.vod.logic.SingerListActivity;
import com.cnhaimei.vod.logic.SongListActivity;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.dq.DQManager;
import com.cnhaimei.vod.util.Util;

/**
 * 网络链接的管理器 <br>
 * 保持网络的持续链接
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-27
 * 
 */
public class NetManager implements EventsListener {
	private static final int ID_DCHEART = 1;
	private static final int ID_DCMANAGE = 2;
	private static final int ID_DQHEART = 3;
	private static final int ID_DQMANAGER = 4;
	private final Object lock = new Object();
	private final static NetManager mG = new NetManager();

	private NetManager() {
		dcHeart = new DCHeart();
		dqHeart = new DQHeart();
		dcManager = new DCManager();
		dqManager = new DQManager();
		EventsManager.G().registerListener(EventsManager.Event.DISCONNECT, this);
	}

	DCHeart dcHeart;
	DQHeart dqHeart;
	DCManager dcManager;
	DQManager dqManager;

	public static NetManager G() {
		return mG;
	}
	
	public boolean isDQManagerAlive() {
//		return dcManager.isConneting();
		return dqManager.isConneting();
	}

//	public void initNet() {
//		EventsManager.G()
//				.registerListener(EventsManager.Event.DISCONNECT, this);
//		dcHeart = new DCHeart();
//		dqHeart = new DQHeart();
//		dcManager = new DCManager();
//		dqManager = new DQManager();
//	}

	public int startDCHeart() {
		dcHeart.setId(ID_DCHEART);
		dcHeart.start();
		return ID_DCHEART;
	}
	
	public int startDCHeart(int port) {
		dcHeart.setId(ID_DCHEART);
		dcHeart.setPort(port);
		dcHeart.start();
		return ID_DCHEART;
	}

	public int startDQHeart() {
		dqHeart.setId(ID_DQHEART);
		dqHeart.start();
		return ID_DQHEART;
	}

	public int startDQManger() {
		if (Util.VLOG) {
			VODLog.e("NetManager", "startDQManger()");
		}
		dqManager.setId(ID_DQMANAGER);
		dqManager.start();
		return ID_DQMANAGER;
	}

	public int startDQManager(String ip) {
		if (Util.VLOG) {
			VODLog.e("NetManager", "startDQManger(" + ip +")");
		}
		dqManager.setIp(ip);
		dqManager.setId(ID_DQMANAGER);
		dqManager.start();
		return ID_DQMANAGER;
	}

	public int startDCManager(String ip) {
		dcManager.setIp(ip);
		dcManager.setId(ID_DCMANAGE);
		dcManager.start();
		return ID_DCMANAGE;
	}

	public int getDCHeartId() {
		return dcHeart.getId();
	}

	public int getDQHeartId() {
		return dqHeart.getId();
	}
	
	public int getDQManagerId() {
		if (dqManager != null) {
			return dqManager.getId();
		}
		return ID_DQMANAGER;
	}
	
	public int getDCManangerId() {
		if (dcManager != null) {
			return dcManager.getId();
		}
		return ID_DCMANAGE;
	}
	
	public String getDQManagerIp() {
		return dqManager.getIp();
	}

	public void onEvent(int event, Object obj, boolean isSuc) {
		if (event == EventsManager.Event.DISCONNECT && obj instanceof Integer) {
			int id = (Integer) obj;
			if (id == dcHeart.getId()) {
				dcHeart.start();
			} else if (id == dqHeart.getId()) {
				dqHeart.start();
//			} else if (id == dcManager.getId()) { // 不自动重连
//				dcManager.start();
			} else if (id == dqManager.getId()) {
				SongListActivity.DQManagerDisconnect();
				SingerListActivity.DQManagerDisconnect();
				dqManager.start();
			}
		}
	}

	public void closeAll() {
		if (dqHeart != null) {
			dqHeart.close();
		}
		if (dqManager != null) {
			dqManager.stop();
		}
		if (dcHeart != null) {
			dcHeart.close();
		}
		if (dcManager != null) {
			dcManager.close();
		}
	}
	
	public void close(int id) {
		if (dcManager != null && id == dcManager.getId()) {
			dcManager.close();
		}
		
//		if (dcManager != null && id == dcManager.getId()) {
//			dcManager.close();
//		}
	}

}
